package com.cqupt.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分页工具类
 *
 * @author Tyc
 **/
public class PageUtils {

    /**
     * 对List进行分页，返回当前页的数据
     *
     * @param dataList 总数据
     * @param curPageStr 当前页
     * @param pageSizeStr 每页大小
     * @return 当前页的数据
     */
    public static ArrayList<Map<String, Object>> getDataListByCurPage(List<Map<String, Object>> dataList, String curPageStr, String pageSizeStr) {
        int curPage = Integer.parseInt(curPageStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        int total = dataList.size();
        int max = curPage * pageSize;

        ArrayList<Map<String, Object>> resList = new ArrayList<>();
        if (max <= total) {
            // 未越界，包含最后一页正好显示完
            for (int i = (curPage - 1) * pageSize; i < curPage * pageSize; i++) {
                resList.add(dataList.get(i));
            }
        } else {
            // 最后一页不完整或越界(返回最后一页数据)
            for (int i = (total/pageSize) * pageSize; i < total; i++) {
                resList.add(dataList.get(i));
            }
        }



        return resList;
    }

    /**
     * 得到每次遍历最后的下标
     * @param pageSizeInt
     * @param currentPageInt
     * @param total
     * @return
     */
    public static int getLastIndex(int pageSizeInt,int currentPageInt,int total){
        int lastIndex = 0;
        //最后一页的下标
        int lastPage = total%pageSizeInt==0?(total/pageSizeInt):(total/pageSizeInt)+1;
        if (lastPage==currentPageInt){
            lastIndex = total - 1;
        }else {
            lastIndex =  pageSizeInt*currentPageInt - 1;
        }
        return lastIndex;
    }
}
